package com.zy.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.entity.Email;
import com.zy.entity.JsonResult;
import com.zy.entity.UserSchedule;
import com.zy.mapper.EmailMapper;
import com.zy.service.impl.UserScheduleServiceImpl;
import com.zy.tools.appletsTools.TrimTool;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app")
public class UserScheduleController {

    /**
     * @author 小熊
     * @date 2021-04-10 9:38
     * @param
     * @return null
     */
    @Autowired
    UserScheduleServiceImpl userScheduleService;

    @Autowired
    TrimTool trimTool;
    @Autowired
    EmailMapper emailMapper;

    /**
     * 获取用户指定日程/会议的状态
     *
     * @return java.lang.String
     * @author 小熊
     * @date 2021-04-10 9:41
     */
    @ResponseBody
    @RequestMapping(value = "/getUsersState", produces = "text/plain;charset=utf-8")
    public String getUsersState(String uid, String sid) throws JSONException {
        String[] users = trimTool.trimId(uid);
        List<Map> maps = new ArrayList<>();
        for (int i = 0; i < users.length; i++) {
            Integer userid = Integer.parseInt(users[i]);
            Map map = new HashMap();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id", userid);
            queryWrapper.eq("schedule_id", sid);
            UserSchedule userSchedule = userScheduleService.getOne(queryWrapper);
            if (userSchedule != null) {
                map.put("userId", userSchedule.getUserId());
                map.put("userState", userSchedule.getUserState());
                map.put("remarks", userSchedule.getRemarks());
                maps.add(map);
            }
        }
        String result = JSON.toJSONString(maps);
        return result;
    }

    /**
     * 更新用户的会议日程状态
     *
     * @param uid
     * @param sid
     * @return java.lang.String
     * @author 小熊
     * @date 2021-04-10 15:22
     */
    @ResponseBody
    @RequestMapping("/updateUsersState")
    public JsonResult updateUserState(String uid, String sid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", uid);
        queryWrapper.eq("schedule_id", sid);
        UserSchedule userSchedule = new UserSchedule();
        userSchedule.setUserState(1);

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("remarks", sid);
        queryWrapper1.eq("user_id", uid);
        Email email = new Email();
        email.setRemarks("0");
        emailMapper.update(email, queryWrapper1);

        Boolean result = userScheduleService.update(userSchedule, queryWrapper);
        if (result == true) {
            return new JsonResult("200", "更新完成", "1");
        } else {
            return new JsonResult("200", "更新失败", "-1");
        }

    }
}
